package com.atguigu.array;

public class TestStudent4 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        //学生报到
        students[0] = new Student("张三", 89);
        students[1] = new Student("李四",84);
        students[2] = new Student("王五",90);

        //按照成绩从低到高
        //冒泡排序：（1）相邻元素比较（2）n个元素需要比较n-1轮
        for(int i=1; i<students.length; i++){
            /*
            当i=1，
                students[0] ~ students[1]
                students[1] ~ students[2]
                j = 0,1  j<3-1
            当i=2
                students[0] ~ students[1]
                j=0     j<3-2

                students[j] ~ students[j+1]
             */
            for(int j=0; j<students.length - i; j++){
//                if(students[j] > students[j+1]){//错误， students[j] 和 students[j+1]里面是地址值
                if(students[j].getScore() > students[j+1].getScore()){
                    //交换  students[j] 和 students[j+1]
                    Student temp = students[j];
                    students[j] = students[j+1];
                    students[j+1] = temp;
                }
            }
        }

        //显示排序后的结果
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].display());
        }

    }
}
